/**
 * 定义工具函数
 */
var Tools = {
    //数据类型判断
    isType: function (value, type) {
        var res = Object.prototype.toString.call(value);
        return res === "[object " + type + "]";
    },
    //判断是否为数组且不为空
    isArray: function (data) {
        return Tools.isType(data, "Array");
    },
    //判断是否为对象
    isObj: function (data) {
        return Tools.isType(data, "Object")
    },

    //判断是否为数字
    isNumber: function (data) {
        return Tools.isType(data, "Number")
    },
    //对象值类型判断
    objValType: function (obj) {
        for (var key in obj) {
            if (Tools.isType(obj[key], "Array")) {
                return "Array";
            } else if (Tools.isType(obj[key], "String")) {
                return "String"
            } else if (Tools.isType(obj[key], "Number")) {
                return "Number"
            }
        }
    }
}

function companyTpl(status, Data) {

    var tplStr = "";

    if (status === "success") {

        tplStr += "<p>提交结果：" + Data.success + "</p>";
        Data.result_not ? tplStr += "<p>核查结果：" + Data.result + "</p>" : null;

        if (Data.result_ok) {

            tplStr += "<div class='consequence-section' style='margin-left:-70px;margin-top:11px'>";
            tplStr += "<div class='consequence-title'>企业工商信息</div>";
            tplStr += "<div class='consequence-table-wrap'>"

            tplStr = companyCommonTpl(tplStr, Data);

            tplStr += "</div></div></div>";
        }
    } else if (status === "fail") {
        tplStr += "<p>提交结果：" + Data.success + "</p>"
            + "<p>失败原因：" + Data.reason_desc + "</p>";
    }
    return tplStr;
}

function companyCommonTpl(tplStr, Data) {
    for (var key in Data) {

        var tit = key, curData = Data[key];

        if (Tools.isArray(curData) && curData.length != 0) {

            tplStr += "<div class='company-info' data-order='"+companyInfo[key].order+"'>";
            tplStr += "<h5 class='subTitle'>" + companyInfo[key].describe + "</h5>";
            tplStr += "<div class='lumpContainer'>";

            curData.forEach(function (obj) {
                tplStr += "<div class='consequence-table-inner-wrap'><table class='consequence-table'><tbody>";

                for (var key in obj) {
                    if (!obj[key]) {
                        continue;
                    }
                    tplStr += "<tr><td>" + companyInfo[tit][key] + "</td><td>" + obj[key] + "</td></tr>";
                }
                tplStr += "</tbody></table></div>";
            });

            tplStr += "</div></div>";

        } else if(Tools.isArray(curData) && curData.length == 0){
            tplStr += "<div class='company-info' data-order='"+companyInfo[key].order+"'>";
            tplStr += "<h5 class='subEmptyTitle'>" + companyInfo[key].describe + "</h5>";
            tplStr += "</div>";
        }
        else if (Tools.isObj(curData)) {

            if (Tools.objValType(curData) === "Array") {

                tplStr += "<div class='company-info' data-order='"+companyInfo[key].order+"'>";
                tplStr += "<h5 class='subTitle'>" + companyInfo[key].describe + "</h5>";
                tplStr += "<div class='lumpContainer'>";

                for (var key in curData) {
                    tplStr += "<h5>" + key + "</h5>";

                    curData[key].forEach(function (obj) {
                        tplStr += "<div class='consequence-table-inner-wrap'><table class='consequence-table'><tbody>";
                        for (var key in obj) {
                            if (!obj[key]) {
                                continue;
                            }
                            tplStr += "<tr><td>" + companyInfo[tit][key] + "</td><td>" + obj[key] + "</td></tr>";
                        }
                        tplStr += "</tbody></table></div>";
                    });
                }
                tplStr += "</div></div>";
            }
            else if (Tools.objValType(curData) === "String") {

                tplStr += "<div class='company-info' data-order='"+companyInfo[key].order+"'>";
                tplStr += "<h5 class='subTitle'>" + companyInfo[key].describe + "</h5>";
                tplStr += "<div class='lumpContainer'>";
                tplStr += "<table class='consequence-table'><tbody>";

                for (var key in curData) {
                    if (!curData[key]) {
                        continue;
                    }
                    tplStr += "<tr><td>" + companyInfo[tit][key] + "</td>";
                    tplStr += "<td>" + curData[key] + "</td></tr>";
                }
                tplStr += "</table></div></div>";
            }
        }
        else if (Tools.isNumber(curData)) {
            tplStr += "";
            tplStr += "<div class='company-info' data-order='"+companyInfo[key].order+"'>";
            tplStr += "<h5 class='subTitle'>" + companyInfo[key].describe + "</h5>";
            tplStr += "<div class='lumpContainer'>";
            tplStr += "<table class='consequence-table'><tbody>";
            tplStr += "<tr><td>" + companyInfo[tit][key] + "</td>";
            tplStr += "<td>" + curData + "</td></tr>";
            tplStr += "</table></div></div>";
        }
    }
    return tplStr;
}